package com.syh.jn.scm.service.impl.upstream.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.UpstreamNewGoodsRequest;
import com.syh.jn.scm.domain.supplier.event.UpstreamNewGoodsRequestEvent;
import com.syh.jn.scm.domain.upstream.entity.base.UpBusinessEntityGoodsOffer;
import com.syh.jn.scm.domain.upstream.repository.base.UpBusinessEntityGoodsOfferRepository;
import com.syh.jn.scm.dto.upstream.base.UpBusinessEntityGoodsOfferMto;
import com.syh.jn.scm.dto.upstream.base.UpBusinessEntityGoodsOfferSto;
import com.syh.jn.scm.dto.upstream.base.UpBusinessEntityGoodsOfferVo;
import com.syh.jn.scm.service.api.upstream.base.UpGoodsOfferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 上游供应商商品报价
 */
@Service
@Transactional
public class UpGoodsOfferServiceImpl extends AbstractServiceImpl<UpBusinessEntityGoodsOfferSto, UpBusinessEntityGoodsOfferMto, UpBusinessEntityGoodsOfferVo, UpBusinessEntityGoodsOffer, Long> implements UpGoodsOfferService {
  @Autowired
  private UpBusinessEntityGoodsOfferRepository upBusinessEntityGoodsOfferRepository;

  @Override
  protected AggregateRepository<UpBusinessEntityGoodsOffer, Long> getAggregateRepository() {
    return upBusinessEntityGoodsOfferRepository;
  }

  /**
   * 新增商品报价
   * @param sto
   * @return
   */
  @Override
  public Long save(UpBusinessEntityGoodsOfferSto sto) {
    //新建商品报价业务对象
    UpBusinessEntityGoodsOffer upBusinessEntityGoodsOffer = new UpBusinessEntityGoodsOffer(sto);
    ExtAssert.notNull(upBusinessEntityGoodsOffer, "对象为空");

    //保存到数据库中去
    return upBusinessEntityGoodsOfferRepository.add(upBusinessEntityGoodsOffer);
  }

  /**
   * 新增商品报价
   * @param stoList
   * @return
   */
  @Override
  public void saveList(List<UpBusinessEntityGoodsOfferSto> stoList) {
    stoList.stream().forEach(sto -> {
      //新建商品报价业务对象
      UpBusinessEntityGoodsOffer upBusinessEntityGoodsOffer = new UpBusinessEntityGoodsOffer(sto);
      ExtAssert.notNull(upBusinessEntityGoodsOffer, "对象为空");

      //保存到数据库中去
      upBusinessEntityGoodsOfferRepository.add(upBusinessEntityGoodsOffer);
    });
  }

  /**
   * 消费者：获取商品添加信息
   * @param upstreamNewGoodsRequest
   */
  @DomainEventHandle(UpstreamNewGoodsRequestEvent.UPSTREAMNEWGOODSREQUEST_UPDATE)
  protected void handleUpstreamNewGoodsRequestUpdateEvent(UpstreamNewGoodsRequest upstreamNewGoodsRequest) {
    //新建商品报价业务对象
    UpBusinessEntityGoodsOffer upBusinessEntityGoodsOffer = new UpBusinessEntityGoodsOffer(upstreamNewGoodsRequest);
    ExtAssert.notNull(upBusinessEntityGoodsOffer, "对象为空");

    //保存到数据库中去
    upBusinessEntityGoodsOfferRepository.add(upBusinessEntityGoodsOffer);
  }

  /**
   * 修改商品报价
   * @param mto
   */
  @Override
  public void update(UpBusinessEntityGoodsOfferMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    UpBusinessEntityGoodsOffer goodsOffer = this.getGoodsOffer(mto.getId());
    goodsOffer.modify(mto);

    upBusinessEntityGoodsOfferRepository.update(goodsOffer);
  }

  /**
   * 修改商品报价
   * @param mtoList
   */
  @Override
  public void updateList(List<UpBusinessEntityGoodsOfferMto> mtoList) {
    mtoList.stream().forEach(mto -> {
      //新建商品报价业务对象
      UpBusinessEntityGoodsOffer upBusinessEntityGoodsOffer = upBusinessEntityGoodsOfferRepository.getById(mto.getId());
      ExtAssert.notNull(upBusinessEntityGoodsOffer, "对象为空");

      upBusinessEntityGoodsOffer.modify(mto);
      //保存到数据库中去
      upBusinessEntityGoodsOfferRepository.update(upBusinessEntityGoodsOffer);
    });
  }



  @Override
  public void delete(Long id) {
    UpBusinessEntityGoodsOffer goodsOffer = this.getGoodsOffer(id);
    //goodsOffer.destroy();

    upBusinessEntityGoodsOfferRepository.remove(goodsOffer);
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    //根据id查找商品信息
    UpBusinessEntityGoodsOffer goodsOffer = this.getGoodsOffer(id);

    goodsOffer.enabled();

    upBusinessEntityGoodsOfferRepository.update(goodsOffer);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    //根据id查找商品信息
    UpBusinessEntityGoodsOffer goodsOffer = this.getGoodsOffer(id);

    goodsOffer.disabled();

    upBusinessEntityGoodsOfferRepository.update(goodsOffer);
  }

  /**
   * 根据上游供应商所有通过审核了的关联商品报价
   * @param we
   * @return
   */
  @Override
  public List<UpBusinessEntityGoodsOfferVo> findGoodsOfferByUpStream(Wrapper we) {
    List<UpBusinessEntityGoodsOffer> list = upBusinessEntityGoodsOfferRepository.getList(we);
    List<UpBusinessEntityGoodsOfferVo> voList = new ArrayList<>();
    list.stream().forEach(goodsOffer -> {
      UpBusinessEntityGoodsOfferVo vo = new UpBusinessEntityGoodsOfferVo();
      ExtBeanUtils.copyProperties(goodsOffer, vo);
      voList.add(vo);
    });
    return voList;
  }

  /**
   * 根据id获取商品报价信息
   * @param id
   * @return
   */
  private UpBusinessEntityGoodsOffer getGoodsOffer(Long id) {
    ExtAssert.notNull(id, "id为空");

    UpBusinessEntityGoodsOffer goodsOffer = upBusinessEntityGoodsOfferRepository.getById(id);
    ExtAssert.notNull(goodsOffer, "对象为空");

    return goodsOffer;
  }
}
